﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace MassMail.Components
{
    public class Common
    {
        /// <summary>
        /// 获取GET方式传递的参数
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns>字符串</returns>
        public static string GetParameter(string sParam)
        {

            if (System.Web.HttpContext.Current.Request.QueryString[sParam] != null)
            {
                return System.Web.HttpContext.Current.Request.QueryString[sParam].ToString();

            }
            else if (System.Web.HttpContext.Current.Request.Form[sParam] != null)
            {
                return System.Web.HttpContext.Current.Request.Form[sParam].ToString();
            }
            else
            {
                return "";
            }

        }

        /// <summary>
        /// 获取GET方式传递的参数
        /// </summary>
        /// <param name="sParam">参数名称</param>
        /// <returns>整型</returns>
        public static int GetIntParameter(string sParam)
        {
            int iOut = 0;
            string sOut;
            if (System.Web.HttpContext.Current.Request.QueryString[sParam] != null)
            {
                sOut = System.Web.HttpContext.Current.Request.QueryString[sParam].ToString();
                try
                {
                    if (!IsNullOrEmpty(sOut))
                        iOut = Convert.ToInt32(sOut);
                }
                catch
                { }
            }
            else if (System.Web.HttpContext.Current.Request.Form[sParam] != null)
            {
                sOut = System.Web.HttpContext.Current.Request.Form[sParam].ToString();
                try
                {
                    if (!IsNullOrEmpty(sOut))
                        iOut = Convert.ToInt32(sOut);
                }
                catch
                { }
            }

            return iOut;
        }

        /// <summary>
        /// 是否空值
        /// </summary>
        /// <param name="text">字符串</param>
        /// <returns>bool结果</returns>
        public static bool IsNullOrEmpty(string text)
        {
            if (text == null || (text != null && text.Length == 0))
                return true;

            return false;
        }

        public static string md5(string str)
        {
            return md5(str, 32);
        }
        public static string md5(string str, int code)
        {
            if (code == 16) //16位MD5加密（取32位加密的9~25字符）
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
            }
            else//32位加密
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
            }
        }

        /// <summary>
        ///验证Email格式是否正确
        /// </summary>
        public static bool isValidEmail(string strln)
        {
            return Regex.IsMatch(strln, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

        /// <summary>
        /// 转 Double
        /// </summary>
        /// <param name="value">object</param>
        /// <returns>返回double，转换失败返回0</returns>
        public static double ToDouble(object value)
        {
            double rtn = 0.00;
            if (value.ToString() != "")
            {
                try
                {
                    rtn = Convert.ToDouble(value);
                }
                catch (Exception Ex)
                {
                    LogService.ErrorLog("Common::public static double getDouble(string " + value + ")", Ex);
                }
            }
            return rtn;
        }

        /// <summary>
        /// 转 Int32
        /// </summary>
        /// <param name="value">object</param>
        /// <returns>返回Int32，转换失败返回0</returns>
        public static int ToInt32(object value)
        {
            Int32 rtn = 0;
            if (value.ToString() != "")
            {
                try
                {
                    rtn = Convert.ToInt32(value);
                }
                catch (Exception Ex)
                {
                    LogService.ErrorLog("Common::public static int ToInt32(string " + value + ")", Ex);
                }
            }
            return rtn;
        }

        public static DateTime ToDateTime(object value)
        {
            DateTime rtn = new System.DateTime();
            if (value.ToString() != "")
            {
                try
                {
                    rtn = Convert.ToDateTime(value);
                }
                catch (Exception Ex)
                {
                    LogService.ErrorLog("Common::public static DateTime ToDateTime(object " + value + ")", Ex);
                }
            }
            return rtn;
        }

        public static void Alert(Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "PopupScript", "alert('" + msg + "');", true);
        }

        public static void Location(Page page, string url)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "MyScript", "window.location='" + url + "';", true);
        }
    }
}